home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / BBS / HERMES / ProtMover6.1.cpt / ASCII / Receive.c < prev    next >
Text File  |  1990-06-08  |  4KB  |  164 lines

  1. /*    ASCII Transfer External for Hermes © 1990 By John Raymonds.
  2.  
  3.     Written in THINK C 4.0.1 as an example external for Hermes protocol
  4.     developers.  Use of any code shown below in any program other than
  5.     an external to be used with Hermes requires written permission from
  6.     the author:
  7.     
  8.     John Raymonds
  9.     76174,205 (Compuserve)
  10.     D3885 (AppleLink)
  11.     
  12.     Comments and suggestions are welcome.
  13.     
  14. */
  15.  
  16. #include "Protocol.h"
  17. #include "ASCII.h"
  18.  
  19. extern ProtoRecPtr PRP;
  20. extern ProtoGloPtr PGP;
  21.  
  22. RecvASCII()
  23. {
  24.     OsErr err;
  25.     register ProtoRecPtr prp;
  26.     register ProtoGloPtr pgp;
  27.     int errCount, conErr, Done, byteCount, c;
  28.     pgp = PGP;
  29.     prp = PRP;
  30.     err = noErr;
  31.     conErr = 0;
  32.     if (prp->F.B.transMode != TERMINALMODE) {
  33.         SendString(0L);
  34.         SendString("\pASCII 1.0d6 - Type Control-K to abort,");
  35.         SendString("\por Control-D to Finish");
  36.         SendString(0L);
  37.         FlushWrite();
  38.     }
  39.     Pstrcpy(pgp->fName, *prp->fList[prp->filesDone].fName);
  40.     prp->startTime = TickCount();
  41.     prp->bytesDone = 0;
  42.     /*    since we are doing an ASCII transfer we do not know how long
  43.         the file is.
  44.     */
  45.     prp->bytesTotal = -1L;
  46.     /*    open file */
  47.     pgp->FP.ioCompletion = 0L;
  48.     pgp->FP.ioNamePtr = pgp->fName;
  49.     pgp->FP.ioVRefNum = prp->fList[prp->filesDone].vRefNum;
  50.     pgp->FP.ioFVersNum = 0;
  51.     err = PBCreate(&pgp->FP, FALSE);
  52.     pgp->FP.ioFDirIndex = 0;
  53.     PBGetFInfo(&pgp->FP, FALSE);
  54.     if (!err) {
  55.         pgp->FP.ioFlFndrInfo.fdType = 'TEXT';
  56.         pgp->FP.ioFlFndrInfo.fdCreator = pgp->thePrefs.fType;
  57.         PBSetFInfo(&pgp->FP, FALSE);
  58.         /*    open the data fork */
  59.         pgp->DIOP.ioCompletion = 0L;
  60.         pgp->DIOP.ioNamePtr = pgp->fName;
  61.         pgp->DIOP.ioVRefNum = prp->fList[prp->filesDone].vRefNum;
  62.         pgp->DIOP.ioVersNum = 0;
  63.         pgp->DIOP.ioPermssn = fsRdWrPerm;
  64.         pgp->DIOP.ioMisc = 0L;
  65.         err = PBOpen(&pgp->DIOP, FALSE);
  66.     }
  67.     byteCount = 0;
  68.     Done = FALSE;
  69.     while (!Done) {
  70.         errCount = pgp->errCount;
  71.         c = WaitForChar(15);
  72.         switch(c) {
  73.         case TIMEOUT:
  74.             tellerror(TIMEOUT);
  75.             break;
  76.         case ABORT:
  77.             Done = TRUE;
  78.             tellerror(CANCELED);
  79.             /*    since the transfer was aborted by the sender there is
  80.                 no reason to set errFatal... (he wanted to stop)
  81.             */
  82.             break;
  83.         case FINISHED:
  84.             pgp->DIOP.ioBuffer = (Ptr) &pgp->theData[0];
  85.             pgp->DIOP.ioReqCount = byteCount;
  86.             pgp->DIOP.ioPosMode = fsAtMark;
  87.             err = PBWrite(&pgp->DIOP, FALSE);
  88.             byteCount = 0;
  89.             /*    close file */
  90.             PBClose(&pgp->DIOP, FALSE);
  91.             PBFlushVol(&pgp->DIOP, FALSE);
  92.             pgp->DIOP.ioRefNum = 0;
  93.             /*    tell Hermes that the transfer was successful */
  94.             prp->F.B.newFile = TRUE;
  95.             Return(0, &pgp->SSR);
  96.             ++prp->filesDone;
  97.             /*    stop the loop */
  98.             Done = TRUE;
  99.             break;
  100.         default:
  101.             pgp->theData[byteCount++] = c;
  102.             if (byteCount == 1024) {
  103.                 pgp->DIOP.ioBuffer = (Ptr) &pgp->theData[0];
  104.                 pgp->DIOP.ioReqCount = byteCount;
  105.                 pgp->DIOP.ioPosMode = fsAtMark;
  106.                 err = PBWrite(&pgp->DIOP, FALSE);
  107.                 prp->bytesDone += pgp->DIOP.ioActCount;
  108.                 byteCount = 0;
  109.             }
  110.             break;
  111.         }
  112.         if (errCount != pgp->errCount) {
  113.             conErr++;
  114.             if (conErr >= MAXERR) {
  115.                 pgp->errFatal = TOOMANYERR;
  116.             }
  117.         }
  118.         else {
  119.             conErr = 0;
  120.         }
  121.         if (err || prp->F.B.carrierLost) {
  122.             Done = TRUE;
  123.         }
  124.         if (prp->F.B.stopTrans || (conErr >= MAXERR)) {
  125.             Done = TRUE;
  126.         }
  127.     }
  128.     /*    see if the file is still open */
  129.     if (pgp->DIOP.ioRefNum) {
  130.         /*    close file */
  131.         PBClose(&pgp->DIOP, FALSE);
  132.         PBFlushVol(&pgp->DIOP, FALSE);
  133.         pgp->DIOP.ioRefNum = 0;
  134.         /*    something was wrong if it was still open */
  135.         PBDelete(&pgp->FP, FALSE);
  136.     }
  137.     /*    flush any characters still coming in */
  138.     FlushModemInput();
  139.     if (prp->F.B.transMode != TERMINALMODE) {
  140.         SendString(0L);
  141.         SendString(0L);
  142.         SendString("\pASCII Receive Finished...");
  143.     }
  144.     FlushWrite();
  145.     /*    we are reserving err for Mac OS errors only...
  146.     
  147.         errFatal is keeping track of any protocol erorrs that may
  148.         make the transfer fail.
  149.     */
  150.     if (!err) {
  151.         if (pgp->errFatal) {
  152.             SetError(pgp->errFatal);
  153.         }
  154.     }
  155.     else {
  156.         SetError(err);
  157.     }
  158.     /*    MAKE SURE WE RETURN WITH A NON-ZERO ERROR CODE!!! */
  159.     if (!err) {
  160.         err = 128;
  161.     }
  162.     return(err);
  163. }
  164.